<template>
  <view class="index">
    <!-- 自定义定位导航 -->
    <view class="navTitleBox navTitleBox2">
      <u-status-bar></u-status-bar>
      <view class="navTitle">首页</view>
    </view>
    <view class="search">
      <view class="search-b" @click="goGoodSearch()">
        <u-input
          v-model="keyword"
          placeholder="搜索商品"
          disabled
          class="input"
        >
          <u-image
            slot="prefix"
            src="/static/img/search.png"
            width="32rpx"
            height="32rpx"
            mode="aspectFill"
          ></u-image>
        </u-input>

        <!-- <view
          class="cart"
          @click.stop="
            $yrouter.push({
              path: '/packageShop/pages/shop/ShoppingCart/index',
            })
          "
        >
          <image
            class="image"
            :src="'https://res.jiyaqi.com/mall/2023/11/8/7f463aa271e243098b67f769065bf68d.png'"
          >
          </image>
        </view> -->
      </view>
    </view>
    <view
      class="banner-box"
      :style="{ backgroundColor: index > 3 ? '#f5f5f5' : '#fff' }"
      :class="index == 0 ? 'banner-hide' : ''"
      v-for="(item, index) in homeData"
      :key="index"
    >
      <!-- 2.0.19支持autoBack，默认为false -->
      <!-- <view
        class="titleTips"
        v-if="item.type == 'banner'"
        @click="toPage('public/weAre', false)"
      >
        <view class="left">
          <image
            class="image"
            :src="'https://res.jiyaqi.com/mall/2023/11/7/e4e76466cfaf437087f86a0903efefcf.png'"
          >
          </image>
          <text>正品保障 一站购齐</text>
        </view>
        <view class="right">
          <text>百万宠主的共同选择</text>
          <text class="f16 iconfont icon-more1"></text>
        </view>
      </view> -->

      <Banner
        v-if="item.type == 'banner'"
        :detail="item.componentContent.bannerData"
        @getbgcolor="getbgcolor"
      ></Banner>

      <!--      <uni-notice-bar v-if="item.type == 'noticeBar'" scrollable="true"
		@click="goRoll(item.componentContent.roll[0])" single="true" :speed="10" showIcon="true"
		:text="item.componentContent.roll[0].info">
	  </uni-notice-bar> -->

      <view
        class="content_box home_content_box"
        v-if="item.type == 'menu' && item.componentContent.menus"
      >
        <!-- 菜单 -->
        <Menu :list="item.componentContent.menus"></Menu>
      </view>
      <!-- 滚动新闻 -->
      <!-- 广告 -->
      <!-- <Adv v-if="item.type == 'adv' && item.componentContent.detail" :detail="item.componentContent.detail" /> -->

      <!-- 热门榜单 -->
      <HotCommodity v-if="item.type == 'hotCommodity'" :detail="likeInfo" />

      <!-- 超值拼团 -->
      <Groupon v-if="item.type == 'groupon'" :detail="combinationList" />

      <!-- 首发新品->秒杀 -->
      <FirstNewProduct
        v-if="item.type == 'firstNewProduct'"
        :detail="firstList"
      />

      <!-- 精品推荐 -->
      <ProductsRecommended
        v-if="item.type == 'productsRecommended'"
        :detail="bastList"
      />

      <!-- 促销单品 -->
      <PromoteProduct v-if="item.type == 'promoteProduct'" :detail="benefit" />

      <!-- 直播 -->
      <!-- #ifdef MP-WEIXIN -->
      <!-- <Live v-if="item.type == 'live'" :detail="live" /> -->
      <!-- #endif -->

      <!-- 为您推荐 -->
      <PromotionGood v-if="item.type == 'promotionGood'" :benefit="benefit" />
      <!-- <Coupon-window
        :coupon-list="couponList"
        v-if="showCoupon"
        @checked="couponClose"
        @close="couponClose"
      /> -->
    </view>
    <!-- 优惠券 -->
    <!-- <CouponShop
      v-model="showCoupon"
      :coupon-list="couponList"
      @checked="e => (showCoupon = e)"
      @close="e => (showCoupon = e)"
    /> -->
    <!-- #ifdef H5 -->
    <view class="bottomSpace" style="line-height: 100rpx">
      正在使用H5方式浏览
    </view>
    <!-- #endif -->
  </view>
</template>
<script>
import { mapActions } from 'vuex'
import GoodList from '@/components/GoodList'
import PromotionGood from '@/components/PromotionGood' // 为你推荐
// import CouponWindow from '@/components/CouponWindow'
import Menu from '@/components/Menu'
import Groupon from '@/components/sh-groupon.vue'
import Banner from './components/Banner'
import HotCommodity from './components/HotCommodity'
import FirstNewProduct from './components/FirstNewProduct'
import ProductsRecommended from './components/ProductsRecommended'
// import CouponShop from '@/components/CouponShop.vue'
import { getHomeData, getShare, getCanvas } from '@/api/public'
import { handleUrlParam } from '@/utils/index'
import { openShareAll } from '@/libs/wechat'

export default {
  name: 'Index',
  components: {
    // swiper,
    // swiperSlide,
    GoodList,
    PromotionGood,
    // CouponWindow,
    Menu,
    Groupon,
    Banner,
    HotCommodity,
    FirstNewProduct,
    ProductsRecommended,
    // CouponShop,
    //Live,
  },
  data: function () {
    return {
      titleStyle: {
        fontSize: '16px',
        fontFamily: 'PingFangSC-Medium, PingFang SC',
        fontWeight: '500',
        color: '#000000',
      },
      keyword: '',
      homeData: [],
      CustomBar: this.CustomBar,
      StatusBar: this.StatusBar,
      formatMenus: [],
      categoryCurrent: 0,
      menuNum: 4,
      bgcolor: '',
      bgColor: '',
      swiperCurrent: 0, //轮播下标
      webviewId: 0,
      showCoupon: false, // 弹窗开关
      logoUrl: '',
      banner: [],
      menus: [],
      combinationList: [],
      roll: [],
      activity: [],
      activityOne: {},
      bastList: [],
      firstList: [],
      info: {
        fastList: [],
        bastBanner: [],

        bastList: [],
      },
      likeInfo: [],
      live: [],
      lovely: [],
      benefit: [],
      couponList: [
        {
          id: 51,
          cid: 32,
          cname: '0826',
          ctype: 0,
          startTime: '2024-08-26',
          endTime: '2024-08-29',
          totalCount: 0,
          remainCount: 0,
          isPermanent: 1,
          status: null,
          couponPrice: 1,
          useMinPrice: 100,
          isUse: false,
        },
        {
          id: 60,
          cid: 40,
          cname: '不限量优惠券',
          ctype: 0,
          startTime: '2024-08-27',
          endTime: '2024-08-28',
          totalCount: 3,
          remainCount: 2,
          isPermanent: 1,
          status: null,
          couponPrice: 2,
          useMinPrice: 2.1,
          isUse: false,
        },
        {
          id: 64,
          cid: 33,
          cname: '测试商品2专属优惠券',
          ctype: 1,
          startTime: '2024-08-27',
          endTime: '2024-08-27',
          totalCount: 1,
          remainCount: 1,
          isPermanent: 0,
          status: null,
          couponPrice: 1.99,
          useMinPrice: 2,
          isUse: false,
        },
        {
          id: 51,
          cid: 32,
          cname: '0826',
          ctype: 0,
          startTime: '2024-08-26',
          endTime: '2024-08-29',
          totalCount: 0,
          remainCount: 0,
          isPermanent: 1,
          status: null,
          couponPrice: 1,
          useMinPrice: 100,
          isUse: false,
        },
        {
          id: 60,
          cid: 40,
          cname: '不限量优惠券',
          ctype: 0,
          startTime: '2024-08-27',
          endTime: '2024-08-28',
          totalCount: 3,
          remainCount: 2,
          isPermanent: 1,
          status: null,
          couponPrice: 2,
          useMinPrice: 2.1,
          isUse: false,
        },
        {
          id: 64,
          cid: 33,
          cname: '测试商品2专属优惠券',
          ctype: 1,
          startTime: '2024-08-27',
          endTime: '2024-08-27',
          totalCount: 1,
          remainCount: 1,
          isPermanent: 0,
          status: null,
          couponPrice: 1.99,
          useMinPrice: 2,
          isUse: false,
        },
      ],
      swiperOption: {
        pagination: {
          el: '.swiper-pagination',
          clickable: true,
        },
        autoplay: {
          disableOnInteraction: false,
          delay: 2000,
        },
        loop: true,
        speed: 1000,
        observer: true,
        observeParents: true,
      },
      swiperRoll: {
        direction: 'vertical',
        autoplay: {
          disableOnInteraction: false,
          delay: 2000,
        },
        loop: true,
        speed: 1000,
        observer: true,
        observeParents: true,
      },
      swiperScroll: {
        freeMode: true,
        freeModeMomentum: false,
        slidesPerView: 'auto',
        observer: true,
        observeParents: true,
      },
      swiperBoutique: {
        pagination: {
          el: '.swiper-pagination',
          clickable: true,
        },
        autoplay: {
          disableOnInteraction: false,
          delay: 2000,
        },
        loop: true,
        speed: 1000,
        observer: true,
        observeParents: true,
      },
      swiperProducts: {
        freeMode: true,
        freeModeMomentum: false,
        slidesPerView: 'auto',
        observer: true,
        observeParents: true,
      },
      bgImage: '',
      // indexTitle: false,
      show: false,
      scrollTop: 0,
    }
  },
  //监听屏幕滚动，获取滚动距离
  onPageScroll(e) {
    // let that = this
    // var opacity,
    //   show,
    // console.log(e, 'e-e-e-e-e--ee--e-----')
    // return
    this.scrollTop = e.scrollTop
    //根据我们要的滚动距离设置渐隐渐显
    //滚动小于115时不显示，当大于115小于200时显示并根据距离调节透明度opacity
    //为了渐隐渐显平滑使用距离进行计算
    if (this.scrollTop <= 115) {
      this.show = false
    } else if (this.scrollTop <= 200) {
      this.show = true
      this.opacity = (this.scrollTop - 115) / 100
    } else {
      this.opacity = 1
    }
    // that.setData({
    //   opacity: opacity,
    //   show: show,
    // })
  },
  computed: {
    singNew() {
      return this.roll.length > 0 ? this.roll[0] : '你还没添加通知哦！'
    },
    // customStyle() {
    //   var bgImage = this.bgImage
    //   // var style = `height:${this.CustomBar}px;padding-top:${0}px;background: ${this.bgcolor}`;
    //   var style = `height:${this.CustomBar}px;padding-top:${this.StatusBar}px;background: ${this.bgcolor}`
    //   if (this.bgImage) {
    //     style = `${style}background-image:url(${bgImage});`
    //   }
    //   return style
    // },
  },
  onLoad() {
    this.getLocation()
    // uni.showLoading({
    //   title: "加载中",
    // });
    getCanvas()
      .then(res => {
        console.log('223', res)
      })
      .catch(error => {
        if (!error) {
          return
        }
        this.homeData = JSON.parse(error.data.json)
        console.log('225', this.homeData)
      })
    getHomeData().then(res => {
      this.logoUrl = res.data.logoUrl
      console.log('getHomeData.res', res)
      res.data.banner.forEach(item => (item.bgcolor = item.color || '#1a6874'))
      this.info = res.data.info
      console.log('239', res.data.info)
      console.log('239', this.info)
      this.$set(this, 'info', res.data.info)
      this.$set(this, 'firstList', res.data.firstList)
      this.$set(this, 'bastList', res.data.bastList)
      this.$set(this, 'likeInfo', res.data.likeInfo)
      this.$set(this, 'live', res.data.liveList)
      this.$set(this, 'lovely', res.data.lovely)
      this.$set(this, 'benefit', res.data.benefit)
      this.$set(this, 'couponList', res.data.couponList)
      this.$set(this, 'combinationList', res.data.combinationList)
      uni.hideLoading()
      this.setOpenShare()
      // this.doColorThief()
    })
  },
  onShareTimeline() {
    return {
      title: '爱宠好物啥都有——宠物食品、宠物用品、宠物药品',
      path: '/pages/home/index?spread=' + uni.getStorageSync('uid'),
    }
  },
  onShareAppMessage() {
    return {
      title: '爱宠好物啥都有——宠物食品、宠物用品、宠物药品',
      path: 'pages/home/index?spread=' + uni.getStorageSync('uid'),
    }
  },
  methods: {
    ...mapActions(['getLocation']),
    goRoll(item) {
      if (item.uniapp_url) {
        this.$yrouter.push(item.uniapp_url)
      }
    },
    goGoodSearch() {
      // this.$yrouter.push('/packageShop/pages/shop/GoodsEvaluate/index');
      this.$yrouter.push('/packageShop/pages/shop/GoodSearch/index')
    },
    goWxappUrl(item) {
      this.$yrouter.push(item.uniapp_url)
    },
    goHotNewGoods(type) {
      this.$yrouter.push({
        path: '/packageShop/pages/shop/HotNewGoods/index',
        query: {
          type,
        },
      })
    },
    goGoodsCon(item) {
      this.$yrouter.push({
        path: '/packageShop/pages/shop/GoodsCon/index',
        query: {
          id: item.id,
        },
      })
    },
    goGoodsPromotion() {
      this.$yrouter.push('/packageShop/pages/shop/GoodsPromotion/index')
    },
    setOpenShare: function () {
      if (this.$deviceType == 'weixin') {
        getShare().then(res => {
          var data = res.data.data
          var configAppMessage = {
            desc: data.synopsis,
            title: data.title,
            link: location.href,
            imgUrl: data.img,
          }
          openShareAll(configAppMessage)
        })
      }
    },
    startQr: function () {
      uni.scanCode({
        success: res => {
          let option = handleUrlParam(res.result)
          console.log(option)
          switch (option.pageType) {
            case 'good':
              // 跳转商品详情
              this.$yrouter.push({
                path: '/packageShop/pages/shop/GoodsCon/index',
                query: {
                  q: res.result,
                },
              })
              break
            case 'group':
              // 跳转团购
              this.$yrouter.push({
                path: '/packageShop/pages/activity/GroupRule/index',
                query: {
                  q: res.result,
                },
              })
              break
            case 'dargain':
              // 跳转砍价
              this.$yrouter.push({
                path: '/packageShop/pages/activity/DargainDetails/index',
                query: {
                  q: res.result,
                },
              })
              break
            default:
              // 跳转分销
              this.$yrouter.push({
                path: '/pages/Loading/index',
                query: {},
              })
              break
          }
        },
      })
    },
    getbgcolor(e) {
      this.bgcolor = e
    },
  },
}
</script>
<style lang="scss">
.navTitle {
  text-align: center;
  height: 88rpx;
  padding: 28rpx 32rpx;
  font-size: 32rpx;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 500;
  color: #000000;
  line-height: 32rpx;
}

.navTitleBox2 {
  position: fixed;
  width: 100%;
  left: 0;
  top: 0;
  z-index: 9999999999;
  // background: url('https://res.jiyaqi.com/mall/2023/11/7/b5397775258344e496c9e9cc53cde50f.png')
  //   no-repeat !important;
  // background-size: 100% 330rpx !important;
}
</style>
<style scoped lang="less">
.banner-box{
  padding-top: 16rpx;
  &:first-child{
    display: none;
  }
}
.banner-hide{
  display: none;
}
.content_box {
  background: #f5f5f5;
}

.index {
  background-color: #f5f5f5;
  padding-top: 140rpx;
}

.swiper-item {
  height: 100%;
}

.fixed-header {
  position: fixed;
  z-index: 99;
  // #ifdef H5
  top: 88rpx;
  // #endif

  // #ifndef H5
  top: 0;
  // #endif
  left: 0;
  right: 0;
  background: #fff;
  box-shadow: 0 0 20rpx -10rpx #aaa;

  & + .fixed-header-box {
    height: 98rpx;
  }
}

.head_box {
  width: 750rpx;
  // background: #fff;
  transition: all linear 0.3s;

  /deep/.cuIcon-back {
    display: none;
  }

  .nav-title {
    font-size: 32rpx;
    font-family: PingFang SC;
    font-weight: 500;
    color: #fff;
  }
}

.cu-bar.fixed {
  position: fixed;
  width: 100%;
  top: 0;
  z-index: 1024;
  // box-shadow: 0 1upx 6upx rgba(0, 0, 0, 0.1);
}

.cu-bar {
  box-sizing: border-box;

  .index .header {
    height: 64rpx;
    // width: 100%;
    // padding: 0 30rpx;
    // box-sizing: border-box;
  }
}

.header-search {
  transition: all linear 0.3s;
}

.cu-bar .action {
  display: -webkit-box;
  display: -webkit-flex;
  display: flex;
  align-items: center;
  height: 100%;
  max-height: 100%;

  &:first-child {
    margin-left: 15px;
    font-size: 15px;
  }
}

.home_content_box {
  // margin-top: -20rpx;
}

.head_box {
}

.nav-title {
  margin-left: 20rpx;
  line-height: 40px;
}

.index {
  .uni-noticebar {
    margin-bottom: 0;
  }

  // background: url('https://res.jiyaqi.com/mall/2023/11/7/b5397775258344e496c9e9cc53cde50f.png')
  //   no-repeat;
  // background-size: 100% 400rpx;
  // //设置背景图像是否固定,不随着页面的其余部分滚动。
  // background-attachment: fixed;
  // //设置背景图片不平铺
  // background-repeat: no-repeat;
}

.titleTips {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 34rpx 32rpx 38rpx 32rpx;
  font-size: 24rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #000000;

  .right {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #a6aaa6;

    .f16 {
      margin-top: 2rpx;
      margin-left: 8rpx;
      font-size: 20rpx;
      color: rgba(0, 0, 0, 0.5);
    }
  }

  .left {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .image {
      width: 28rpx;
      height: 28rpx;
      margin-right: 8rpx;
    }
  }
}

.search {
  background-color: #fff !important;
}

.search-b {
  margin-top: 10rpx;
  height: 80rpx;

  .cart {
    margin-left: 24rpx;
    height: 48rpx;
    width: 48rpx;

    .image {
      height: 48rpx;
      width: 48rpx;
    }
  }
}

::v-deep .u-input.data-v-113bc24f {
  border-radius: 34rpx !important;
  background-color: #F5F6F7 !important;
}

::v-deep .u-navbar--fixed.data-v-95dec1ae {
  background: linear-gradient(135deg, #dcf4fa 0%, #edf8e2 100%) !important;
}
</style>
